"""
爬虫工厂模块

根据配置创建不同类型的爬虫实例。
"""

from typing import Dict, Type
from ..models.data_model import WebsiteConfig
from .base_scraper import BaseScraper
from .zjzwfw_scraper import ZJZWFWScraper
from ..utils.logger import get_logger


class ScraperFactory:
    """爬虫工厂类"""

    # 注册的爬虫类型
    _scrapers: Dict[str, Type[BaseScraper]] = {
        'zjzwfw': ZJZWFWScraper,
    }

    @classmethod
    def register_scraper(cls, scraper_type: str, scraper_class: Type[BaseScraper]) -> None:
        """
        注册新的爬虫类型

        Args:
            scraper_type: 爬虫类型标识
            scraper_class: 爬虫类
        """
        cls._scrapers[scraper_type] = scraper_class

    @classmethod
    def create_scraper(cls, config: WebsiteConfig) -> BaseScraper:
        """
        创建爬虫实例

        Args:
            config: 网站配置

        Returns:
            爬虫实例

        Raises:
            ValueError: 当爬虫类型不支持时
        """
        scraper_type = config.scraper_type

        if scraper_type not in cls._scrapers:
            available_types = ', '.join(cls._scrapers.keys())
            raise ValueError(
                f"不支持的爬虫类型: {scraper_type}. "
                f"支持的类型: {available_types}"
            )

        scraper_class = cls._scrapers[scraper_type]
        return scraper_class(config)

    @classmethod
    def get_available_scrapers(cls) -> Dict[str, Type[BaseScraper]]:
        """
        获取所有可用的爬虫类型

        Returns:
            爬虫类型字典
        """
        return cls._scrapers.copy()
